<div class="content">
    <div id="example_title">
        <h1>Collapsible Groups</h1>
        Groups can be collapsed, expanded, and toggled.
    </div>
    <div id="example_view"></div>
    <div id="example_code"></div>
</div>

<!--CODE-->
<div id="form" style="width: 750px; height: 500px"></div>
<br>
<button class="w2ui-btn" onclick="toggleGroup('General')">Toggle General</button>
<button class="w2ui-btn" onclick="toggleGroup('Address Group 2')">Toggle Address Group 2</button>
<button class="w2ui-btn" onclick="toggleGroup('Address Group 3', false)">Collapse Address Group 3</button>
<button class="w2ui-btn" onclick="toggleGroup('Address Group 3', true)">Expand Address Group 3</button>
<br>

<!--CODE-->
<script type="module">
import { w2form, w2ui, w2popup, w2alert } from '__W2UI_PATH__'

let form = new w2form({
    box: '#form',
    name: 'form',
    url: 'server/post',
    fields: [
        { field: 'first_name', type: 'text',
            html: {
                group: 'General',
                groupCollapsible: true,
                groupCollapsable: true, // misspelled in 1.5.0
                page: 0,
                column: 0,
                label: 'First Name',
                span: 6
            }
        },
        { field: 'last_name', type: 'text',
            html: {
                page: 0,
                column: 0,
                label: 'Last Name',
                span: 6
            }
        },
        { field: 'middle_name',  type: 'text',
            html: {
                page: 0,
                column: 0,
                label: 'Middle Name',
                span: 6
            }
        },
        { field: 'Other', type: 'textarea',
            html: {
                page: 0,
                column: 1,
                group: 'Additional Info',
                groupCollapsible: true,
                groupCollapsable: true, // misspelled in 1.5.0
                groupStyle: 'min-height: 140px',
                label: 'Other',
                span: -1,
                attr: 'style="width: 100%; height: 90px; resize: vertical"'
            }
        },
        { field: 'address1', type: 'text',
            html: {
                page: 0,
                column: 0,
                group: 'Address Group 1',
                groupCollapsible: false,
                groupCollapsable: true, // misspelled in 1.5.0
                label: 'Address',
            }
        },
        { field: 'address2', type: 'text',
            html: {
                page: 0,
                label: 'Line 2',
                column: 0
            }
        },
        { field: 'city', type: 'text',
            html: {
                page: 0,
                group: 'Address Group 2',
                groupCollapsible: true,
                groupCollapsable: true, // misspelled in 1.5.0
                groupStyle: 'height: 197px',
                label: 'City',
                column: 1,
            }
        },
        { field: 'state', type: 'text',
            html: {
                page: 0,
                label: 'State',
                column: 1
            }
        },
        { field: 'zip', type: 'int',
            html: {
                page: 0,
                label: 'Zip',
                column: 1
            }
        },
        { field: 'country', type: 'text',
            html: {
                page: 0,
                group: 'Address Group 3',
                groupCollapsible: true,
                groupCollapsable: true, // misspelled in 1.5.0
                label: 'Country',
                column: 0,
            }
        }
    ],
    actions: {
        Reset() {
            this.clear()
        },
        Save() {
            if (form.validate().length == 0) {
                w2popup.open({
                    title: 'Form Data',
                    with: 600,
                    height: 550,
                    body: `<pre>${JSON.stringify(this.getCleanRecord(), null, 4)}</pre>`,
                    actions: { Ok: w2popup.close }
                })
            }
        },
        custom: {
            text: '<span style="font-size: 16px">←</span> click to see data',
            class: 'custom-class',
            style: 'background-image: none; background-color: transparent; border: 0px; margin: 0 0 0 -10px;',
            onClick() {
                w2alert('Not me!! The other button')
            }
        }
    }
})

window.toggleGroup = function(txt, flag) {
    w2ui.form.toggleGroup(txt, flag)
}
</script>
